<template>
  <div class="dashboard">
    <!-- 统计卡片 -->
    <div class="stats-cards">
      <el-row :gutter="25">
        <el-col :span="4">
          <el-card class="stat-card">
            <div class="stat-content">
              <div class="stat-icon user-icon">
                <el-icon>
                  <User/>
                </el-icon>
              </div>
              <div class="stat-info">
                <div class="stat-number">{{ stats.userCount }}</div>
                <div class="stat-label">用户总数</div>
              </div>
            </div>
          </el-card>
        </el-col>
        <el-col :span="4">
          <el-card class="stat-card">
            <div class="stat-content">
              <div class="stat-icon role-icon">
                <el-icon>
                  <UserFilled/>
                </el-icon>
              </div>
              <div class="stat-info">
                <div class="stat-number">{{ stats.roleCount }}</div>
                <div class="stat-label">角色总数</div>
              </div>
            </div>
          </el-card>
        </el-col>
        <el-col :span="4">
          <el-card class="stat-card">
            <div class="stat-content">
              <div class="stat-icon permission-icon">
                <el-icon>
                  <Lock/>
                </el-icon>
              </div>
              <div class="stat-info">
                <div class="stat-number">{{ stats.permissionCount }}</div>
                <div class="stat-label">权限总数</div>
              </div>
            </div>
          </el-card>
        </el-col>
        <el-col :span="4">
          <el-card class="stat-card">
            <div class="stat-content">
              <div class="stat-icon menu-icon">
                <el-icon>
                  <Menu/>
                </el-icon>
              </div>
              <div class="stat-info">
                <div class="stat-number">{{ stats.menuCount }}</div>
                <div class="stat-label">菜单总数</div>
              </div>
            </div>
          </el-card>
        </el-col>
        <el-col :span="4">
          <el-card class="stat-card">
            <div class="stat-content">
              <div class="stat-icon active-icon">
                <el-icon>
                  <Star/>
                </el-icon>
              </div>
              <div class="stat-info">
                <div class="stat-number">{{ stats.activeUsers }}</div>
                <div class="stat-label">活跃用户</div>
              </div>
            </div>
          </el-card>
        </el-col>
      </el-row>
    </div>

    <!-- 系统信息和图表 -->
    <el-row :gutter="20" style="margin-top: 20px;">
      <el-col :span="12">
        <el-card class="info-card">
          <template #header>
            <div class="card-header">
              <span>系统信息</span>
            </div>
          </template>
          <div class="info-list">
            <div class="info-item">
              <span class="info-label">系统名称：</span>
              <span class="info-value">福智生-生态数据管理系统</span>
            </div>
            <div class="info-item">
              <span class="info-label">系统版本：</span>
              <span class="info-value">v1.0.0</span>
            </div>
            <div class="info-item">
              <span class="info-label">技术栈：</span>
              <span class="info-value">Flask + Vue3 + MySQL</span>
            </div>
            <div class="info-item">
              <span class="info-label">数据库状态：</span>
              <span class="info-value">
                <el-tag :type="systemInfo.database_connected ? 'success' : 'danger'">
                  {{ systemInfo.database_connected ? '正常' : '异常' }}
                </el-tag>
              </span>
            </div>
            <div class="info-item">
              <span class="info-label">最后备份：</span>
              <span class="info-value">{{ systemInfo.last_backup || '未备份' }}</span>
            </div>
            <div class="info-item">
              <span class="info-label">运行时间：</span>
              <span class="info-value">{{ systemInfo.uptime || '未知' }}</span>
            </div>
            <div class="info-item">
              <span class="info-label">当前用户：</span>
              <span class="info-value">{{ userStore.userInfo?.real_name || userStore.userInfo?.username }}</span>
            </div>
            <div class="info-item">
              <span class="info-label">登录时间：</span>
              <span class="info-value">{{ formatDate(userStore.userInfo?.last_login) }}</span>
            </div>
          </div>
        </el-card>
      </el-col>

      <el-col :span="12">
        <el-card class="info-card">
          <template #header>
            <div class="card-header">
              <span>快速操作</span>
            </div>
          </template>
          <div class="quick-actions">
            <el-button type="primary" @click="$router.push('/users')">
              <el-icon>
                <User/>
              </el-icon>
              用户管理
            </el-button>
            <el-button type="success" @click="$router.push('/roles')">
              <el-icon>
                <UserFilled/>
              </el-icon>
              角色管理
            </el-button>
            <el-button type="warning" @click="$router.push('/permissions')">
              <el-icon>
                <Lock/>
              </el-icon>
              权限管理
            </el-button>
            <el-button type="info" @click="$router.push('/menus')">
              <el-icon>
                <Menu/>
              </el-icon>
              菜单管理
            </el-button>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <!-- 角色权限统计 -->
    <el-row :gutter="20" style="margin-top: 20px;">
      <el-col :span="24">
        <el-card class="chart-card">
          <template #header>
            <div class="card-header">
              <span>角色权限分布</span>
            </div>
          </template>
          <div class="role-stats">
            <el-table :data="rolePermissionStats" style="width: 100%">
              <el-table-column prop="role_name" label="角色名称"/>
              <el-table-column prop="permission_count" label="权限数量"/>
              <el-table-column prop="user_count" label="用户数量"/>
            </el-table>
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup>
import {ref, onMounted} from 'vue'
import {useUserStore} from '@/stores/user'
import {getDashboardStats, getUserActivity, getSystemOverview} from '@/api/stats'
import {ElMessage} from 'element-plus'
import {User, UserFilled, Lock, Menu, Star} from '@element-plus/icons-vue'

const userStore = useUserStore()

const stats = ref({
  userCount: 0,
  roleCount: 0,
  permissionCount: 0,
  menuCount: 0,
  activeUsers: 0
})

const loading = ref(false)
const systemInfo = ref({
  database_connected: true,
  last_backup: '',
  uptime: ''
})
const userGrowth = ref([])
const rolePermissionStats = ref([])

// 格式化日期
const formatDate = (dateString) => {
  if (!dateString) return '未登录'
  return new Date(dateString).toLocaleString('zh-CN')
}

// 获取统计数据
// todo 将数据持久化，避免每次都请求；数据有修改时，更新数据
const getStats = async () => {
  try {
    loading.value = true

    // 获取仪表板统计数据
    const dashboardResponse = await getDashboardStats()
    const dashboardData = dashboardResponse.data

    // 更新基础统计数据
    stats.value = {
      userCount: dashboardData.basic_stats.user_count,
      roleCount: dashboardData.basic_stats.role_count,
      permissionCount: dashboardData.basic_stats.permission_count,
      menuCount: dashboardData.basic_stats.menu_count,
      activeUsers: dashboardData.basic_stats.active_users
    }

    // 更新系统信息
    systemInfo.value = dashboardData.system_status

    // 更新用户增长数据
    userGrowth.value = dashboardData.user_growth

    // 更新角色权限统计
    rolePermissionStats.value = dashboardData.role_permission_stats

    ElMessage.success('统计数据加载成功')

  } catch (error) {
    console.error('获取统计数据失败:', error)
    ElMessage.error('获取统计数据失败')

    // 如果API调用失败，使用默认数据
    stats.value = {
      userCount: 0,
      roleCount: 0,
      permissionCount: 0,
      menuCount: 0,
      activeUsers: 0
    }
  } finally {
    loading.value = false
  }
}

onMounted(() => {
  getStats()
})
</script>

<style scoped>
.dashboard {
  padding: 20px;
}

.stats-cards {
  margin-bottom: 20px;
}

.stat-card {
  height: 120px;
}

.stat-content {
  display: flex;
  align-items: center;
  height: 100%;
}

.stat-icon {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 24px;
  color: white;
  margin-right: 15px;
}

.user-icon {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.role-icon {
  background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
}

.permission-icon {
  background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
}

.menu-icon {
  background: linear-gradient(135deg, #43e97b 0%, #38f9d7 100%);
}

.active-icon {
  background: linear-gradient(135deg, #fa709a 0%, #fee140 100%);
}

.stat-info {
  flex: 1;
}

.stat-number {
  font-size: 28px;
  font-weight: bold;
  color: #333;
  margin-bottom: 5px;
}

.stat-label {
  font-size: 14px;
  color: #666;
}

.info-card {
  height: 300px;
}

.card-header {
  font-weight: bold;
  color: #333;
}

.info-list {
  padding: 10px 0;
}

.info-item {
  display: flex;
  justify-content: space-between;
  padding: 10px 0;
  border-bottom: 1px solid #f0f0f0;
}

.info-item:last-child {
  border-bottom: none;
}

.info-label {
  color: #666;
  font-weight: 500;
}

.info-value {
  color: #333;
}

.quick-actions {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.quick-actions .el-button {
  width: 100%;
  height: 45px;
  font-size: 16px;
  margin-left: 1px;
}

.quick-actions .el-button .el-icon {
  margin-right: 8px;
}

.chart-card {
  margin-top: 20px;
}

.role-stats {
  padding: 10px 0;
}
</style> 